<!DOCTYPE HTML PUBLIC "-//ORA//DTD CD HTML 3.2//EN">
<HTML>
<HEAD>
<TITLE>[Chapter 7] 7.5 Applications</TITLE>
<META NAME="author" CONTENT="Mark Grand">
<META NAME="date" CONTENT="Thu Jul 31 13:15:45 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="java">
<META NAME="title" CONTENT="Java Language Reference">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java Language Reference" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch07_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 7<br>Program Structure</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch07_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JLR2-CH-7-SECT-5">7.5 Applications</A></h2>

<P CLASS=para>
<A NAME="CH07.APP"></A>For a Java program to run
as an application, it must have at least one <tt CLASS=literal>public</tt>
class that contains a <tt CLASS=literal>public static</tt>
method called <tt CLASS=literal>main()</tt> that takes exactly one parameter,
an array of <tt CLASS=literal>String</tt> objects.

<P CLASS=para>
Here is a very simple
sample application that outputs "Hello World!" and then exits:

<DIV CLASS=screen>
<P>
<PRE>
class DoIt {
    public static void main  (String argv[]){
         System.out.println ("Hello World!");
    }
}
</PRE>
</DIV>

<P CLASS=para>
The <tt CLASS=literal>main()</tt> method must be <tt CLASS=literal>public</tt>
so that the Java virtual machine can find it. If the method
is not <tt CLASS=literal>public</tt>, its name is not included in the
compiler's output. The system does not create any objects prior
to the start of the application's <tt CLASS=literal>main()</tt> method,
so the <tt CLASS=literal>main()</tt> method must be <tt CLASS=literal>static</tt>
because it cannot be associated with an object.

<P CLASS=para>
If an application has a graphical user interface, then it typically creates
a <tt CLASS=literal>java.awt.Frame</tt> object in <tt CLASS=literal>main()</tt>.
The <tt CLASS=literal>Frame</tt> object acts as the top-level window
for the application.

<P CLASS=para>
In Sun's implementation of Java,
you run a Java application by running the Java interpreter with
a command-line argument that specifies the name of the class that
contains the <tt CLASS=literal>main()</tt>. The name of the Java interpreter
is <I CLASS=emphasis>java</I>. Here's the command-line for our sample
application:

<DIV CLASS=screen>
<P>
<PRE>
C:\&gt; java DoIt
</PRE>
</DIV>

<P CLASS=para>
The capitalization
of the class name on the command line must match the capitalization
of the class name within the program. If the class is part of a
named package, the name of the class must be qualified with the
package name. For example, if you have a package
called <tt CLASS=literal>COM.geomaker</tt>
and it contains the class called <tt CLASS=literal>DoIt</tt>, you would
use the following command to run the application:

<DIV CLASS=screen>
<P>
<PRE>
C:\&gt; java COM.geomaker.DoIt
</PRE>
</DIV>

<P CLASS=para>
Any additional information that you provide on the command line is passed
to the application as command line arguments. These arguments are
passed to the application using the <tt CLASS=literal>String</tt> array
passed to <tt CLASS=literal>main()</tt>. The number of elements in the
array is equal to the number of arguments passed to the application.
If there are no arguments to the application, the length of the
array passed to <tt CLASS=literal>main()</tt> is zero.

<P CLASS=para>
<b>References</b>
<A HREF="ch05_04.htm#JLR2-CH-5-SECT-4.4.2">Methods</A>;
<A HREF="ch07_02.htm#JLR2-CH-7-SECT-2">Packages</A>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch07_04.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch07_06.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Documentation Comments</td>
<td width=171 align=center valign=top><a href="index/idx_0.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>Applets</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
